﻿using HomagChina.CCT.BaseUI.DataModel;
using HomagChina.CCT.BaseUI.ViewModel;
using HomagChina.CCT.BaseUI.Views;
using HomagGroup.Base.UI;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using localText = HomagChina.Basic.CCT.ResourceLibrary.Properties.Resources;

namespace HomagChina.CCT.BaseUI.ViewModel
{
    public class SettingsViewModel : HomagGroup.Base.UI.NotifyPropertyChangedBase
    {
        #region Handle

        //private CommonHandle commonHandle;
        //private DeleteInfoHandle Handle_Delete;
        //private SearchInfoHandle Handle_Search;

        public RelayCommand Handle_test { get; }

        private string _Ip;

        #endregion Handle

        #region 属性

        public RelayCommand PlusCommand { get; }
        public RelayCommand RefreshCommand { get; }
        public ObservableCollection<SetttingsModel> ItemList { get; }
        public RelayCommand EditCommand { get; }
        public RelayCommand DeleteCommand { get; }
        public RelayCommand SearchCommand { get; }

        public string Ip
        {
            get => _Ip;
            set { _Ip = value; OnPropertyChanged(); }
        }

        private ObservableCollection<SetttingsModel> _List_SettingsInfoMode;

        public ObservableCollection<SetttingsModel> List_SettingsInfoMode
        {
            get => _List_SettingsInfoMode;
            set { _List_SettingsInfoMode = value; OnPropertyChanged(); }
        }

        #endregion 属性

        #region 构造函数

        public SettingsViewModel()
        {
            //commonHandle = new CommonHandle();
            //Handle_Delete = new DeleteInfoHandle();
            //Handle_Search = new SearchInfoHandle();

            PlusCommand = new RelayCommand(OpenDialog);
            RefreshCommand = new RelayCommand(Refresh);
            EditCommand = new RelayCommand(Edit);
            DeleteCommand = new RelayCommand(Delete);
            SearchCommand = new RelayCommand(QueryById);
            Handle_test = new RelayCommand(test);
            Refresh();
        }

        #endregion 构造函数

        #region 函数

        /// <summary>
        /// 条件查询
        /// </summary>
        private void QueryById()
        {
            if (this.Ip != null)
            {
                //var Result = Handle_Search.GetSettingsInfoById(this.Ip);
                //if (Result != null) List_SettingsInfoMode = Result;
            }
            else HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Sorry__please_enter_a_filter_);
        }

        public void test()
        {
            //HomagGroup.Base.UI.Windows.MessageBox.Show("test");
        }

        /// <summary>
        /// 刷新
        /// </summary>
        public void Refresh()
        {
            //List_SettingsInfoMode = Handle_Search.GetSettingsInfo();
            //
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        private void Delete(object obj)
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Are_you_sure_you_want_to_delete_this_recipe + "\r\n", localText.Prompt_message, MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                SetttingsModel list = (SetttingsModel)obj;
                //if (Handle_Delete.DeletedSettingInfo(list.Id))
                //{
                //    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Deleted_successfully);
                //    this.Refresh();
                //}
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="SelectItem"></param>
        private void Edit(object SelectItem)
        {
            var SelectedItem = SelectItem as SetttingsModel;
            if (SelectedItem != null)
            {
                SettingsDialogViewModel settingsDialogViewModel = new SettingsDialogViewModel(SelectedItem, true);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsDialog>(settingsDialogViewModel, null);
            }
        }

        /// <summary>
        ///
        /// </summary>
        private void OpenDialog()
        {
            SettingsDialogViewModel SettingsDialogViewModel = new SettingsDialogViewModel(null, false);
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsDialog>(SettingsDialogViewModel, null);
        }
    }

    #endregion 函数

    /// <summary>
    /// 转换器
    /// </summary>
    public class VisibleIsVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value == 0)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 转换器
    /// </summary>
    public class CollapsedVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value == 1)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}